package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IBrandDAO;
import com.twentyOne.ClothingLand.vo.Brand;
import com.twentyOne.ClothingLand.vo.Size;

public class BrandDAO extends BaseDAO implements IBrandDAO {

	@Override
	public void addBrand(Brand brand) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(brand);
		tx.commit();
		session.close();
		
	}

	@Override
	public void deleteBrand(Brand brand) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "delete Brand as b where b.brandId = "+brand.getBrandId()+"";
		Query query = session.createQuery(hql);
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public void modifyBrand(Brand brand) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update Brand set name=:name, description=:description where brandId=:brandId";
		Query query = session.createQuery(hql);
		query.setString("name", brand.getName());
		query.setString("description", brand.getDescription());
		query.setInteger("brandId", brand.getBrandId());
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public List<Brand> searchAllBrand() {
		
		Session session = getSession();
		String hql = "from Brand";
		Query query = session.createQuery(hql);
		List<Brand> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

}
